﻿using System;
using System.Collections.Generic;
using ClownFish;
using SGDemo.OA.Entity;

namespace SGDemo.OA.DataAccess
{
    /// <summary>
    /// File: PADContactDAL.cs 数据访问类
    /// </summary>
    /// <remarks>
    /// <para>Author: "Ralph"</para>
    /// <para>Date: "2012-11-10"</para>
    /// <para>Version: "V1.0"</para>
    /// <para>Copyright: "神冠软件"</para>
    /// </remarks>
    public class PADContactDAL : DbContextHolderBase
    {
        /// <summary>
        /// 新增
        /// </summary>
        public bool PADContact_Add(PADContactENT ent)
        {
            return (DbHelper.ExecuteNonQuery("sgdemo_oa_PADContact_Add", ent, this.DbContext, CommandKind.StoreProcedure) > 0);
        }

        /// <summary>
        /// 删除
        /// </summary>
        public bool PADContact_Delete(string keyid)
        {
            var parameter = new { Keyid = keyid };
            return (DbHelper.ExecuteNonQuery("sgdemo_oa_PADContact_Delete", parameter, this.DbContext, CommandKind.StoreProcedure) > 0);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public bool PADContact_Update(PADContactENT ent)
        {
            return (DbHelper.ExecuteNonQuery("sgdemo_oa_PADContact_Update", ent, this.DbContext, CommandKind.StoreProcedure) > 0);
        }

        /// <summary>
        /// 根据Keyid查询
        /// </summary>
        public PADContactENT PADContact_GetByKeyid(string keyid)
        {
            var parameter = new { Keyid = keyid };
            return DbHelper.GetDataItem<PADContactENT>("sgdemo_oa_PADContact_GetByKeyid", parameter, this.DbContext, CommandKind.StoreProcedure);
        }

        /// <summary>
        /// 验证排序参数，如果包含非法字符，则抛出异常
        /// </summary>
        private string VerifyAndSetSortColumns(string sortColumns)
        {
            string result = sortColumns;
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');
            foreach (String columnName in columnNames)
            {
                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "contactname":
                    case "sex":
                    case "email":
                    case "officephone":
                    case "mobilephone":
                    case "homephone":
                    case "remark": break;
                    case "": result = "CreateDate ASC"; break;
                    default: throw new ArgumentException("无效的排序列名");
                }
            }
            return result;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        public List<PADContactENT> PADContact_PagingGet(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            sortColumns = VerifyAndSetSortColumns(sortColumns);
            var parameter = new { StartRecord = startRecord, MaxRecords = maxRecords, SortColumns = sortColumns, FilterConditions = filterConditions };
            return DbHelper.FillList<PADContactENT>("sgdemo_oa_PADContact_PagingGet", parameter, this.DbContext, CommandKind.StoreProcedure);
        }

        /// <summary>
        /// 查询页数
        /// </summary>
        public int PADContact_PagingGetCount(String filterConditions)
        {
            var parameter = new { FilterConditions = filterConditions };
            return DbHelper.ExecuteScalar<Int32>("sgdemo_oa_PADContact_PagingGetCount", parameter, this.DbContext, CommandKind.StoreProcedure);
        }
    }
}


